import { GATEWAY_IP, PORT } from './address'
import httpRequest from '../utils/httpRequest'

/**
 * 
 * 保存订单
 * 
 * @param {*} data 
 */
export const saveOrder = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/save`,
            method: 'post',
            data: data
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 
 * 获取零售商订单
 */
export const getOrderList = (current) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/list/${current}`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}


/**
 * 获取供应商订单
 */
export const getSupplierOrderList = (current,begin,end) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/supplierorderlist/${current}`,
            method: 'get',
            params : {
                begin,
                end
            }
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 
 * 合并订单，订单配送
 * 
 * @param {*} data 
 */
export const distribution = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/distribution`,
            method: 'post',
            data: data
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 计算购买价格
 * 
 * @param {*} data 
 * @returns 
 */
export const calculate = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/calculate?money=${data.price}&num=${data.num}`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取合并订单列表
 * 
 * @param {*} current 
 * @returns 
 */
export const getDistributionList = (current) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/distribution/list/${current}`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 根据ID查订单
 * 
 * @param {*} data 
 * @returns 
 */
export const getOrderByIds = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/listbyids`,
            method: 'post',
            data: data
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 订单完成（多个）
 * 
 * @param {*} data 
 * @returns 
 */
export const getOrderComplete = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/ordercomplete`,
            method: 'post',
            data: data
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 订单完成（单个）
 * 
 * @param {*} id 
 * @returns 
 */
export const overOrder = (id) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/distribution/overorder/${id}`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 取消订单
 * 
 * @param {*} id 
 * @returns 
 */
export const cancel = (id) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/cancel/${id}`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 设置零售价格
 * 
 * @param {*} data 
 * @returns 
 */
export const setRetailPrice = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/setretailprice`,
            method: 'get',
            params: {
                id: data.id,
                price: data.price
            }
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 设置商品出售数量
 * 
 * @param {*} data 
 * @returns 
 */
export const changeNum = (data) => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/setnum`,
            method: 'get',
            params: {
                id: data.id,
                num: data.num
            }
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 零售商周统计
 * 
 * @returns 
 */
export const getWeek = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/retailweek/week`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取供应商工作台三个卡片信息
 * 
 * @returns 
 */
export const getCard = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/card`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取订单昨日成交和历史成交
 * 
 * @returns 
 */
export const getOrderCount = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/getOrderCount`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取零售商盈亏统计
 * 
 * @returns 
 */
export const getProfitAndLoss = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/profitAndLoss`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取零售商销售统计
 * 
 * @returns 
 */
 export const getFunnel = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/funnel`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取供应商订单量统计
 * 
 * @returns 
 */
 export const getSupplyWeekOrders = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/getSupplyWeekOrders`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取供应商一周销售额
 * 
 * @returns 
 */
 export const getSupplyWeekMoney = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/getSupplyWeekMoney`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}
/**
 * 获取供应商主页统计
 * 
 * @returns 
 */
 export const getSupplyMain = () => {
    return new Promise((resolve, reject) => {
        httpRequest({
            url: `http://${GATEWAY_IP}:${PORT}/gateway/order/order/getSupplyMain`,
            method: 'get',
        }).then(res => {
            resolve(res.data)
        }).catch(err => {
            reject(err)
        })
    })
}